﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeGenerator
{
    internal static class Program
    {
        public static string errorpath = Path.Combine(Application.StartupPath, "log");

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message + "\r\n 详情请查看日志!", "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            if (!Directory.Exists(errorpath))
            {
                Directory.CreateDirectory(errorpath);
            }
            string path = Path.Combine(errorpath, DateTime.Now.ToString("yyyyMM") + ".log");
            StringBuilder builder = new StringBuilder();
            builder.Append("DateTime:");
            builder.Append(DateTime.Now.ToString());
            builder.Append("\r\n");
            builder.Append("Message:");
            builder.Append(e.Exception.Message);
            builder.Append("\r\n");
            builder.Append("Source:");
            builder.Append(e.Exception.Source);
            builder.Append("\r\n");
            builder.Append("StackTrace:");
            builder.Append(e.Exception.StackTrace);
            builder.Append("\r\n--------------------------------------------------------------\r\n");
            File.AppendAllText(path, builder.ToString());
        }

        [STAThread]
        static void Main()
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Program.Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }
    }
}
